package com.pt25.base.dep.nacos.rpc.aspect;

import com.pt25.base.dep.nacos.rpc.RpcProperties;
import com.pt25.base.dep.nacos.rpc.ddo.RpcData;
import com.pt25.base.exception.BizException;
import com.pt25.base.util.JsonUtil;
import com.pt25.base.util.TokenUtil;
import com.pt25.base.web.WebThreadLocal;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class RpcCheckAspect {

    @Resource
    private RpcProperties rpcProperties;

    @Before("@annotation(com.pt25.base.dep.nacos.rpc.aspect.RpcCheck)")
    public void check() {
        try {
            String rpcToken = WebThreadLocal.get().getRpcToken();
            RpcData rpcData = TokenUtil.decryptToken(rpcToken, rpcProperties.getSecretKey(), RpcData.class);
            log.info("rpcData={}", JsonUtil.toJson(rpcData));
        } catch (Exception e) {
            log.error("rpc-check-error", e);
            throw new BizException("rpc token check fail!");
        }
    }
}
